﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using RuleCode.Table;

namespace chatblog
{
    public partial class blogmanage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                cbuser.dealcookie();
                dealrequest();
                bind1();
                bind2();
                //bind3();
            }
        }
        /// <summary>
        /// 绑定分类
        /// </summary>
        private void bind1()
        {
            cbusercata ct = cbusercata.Find(" cblayer = 1 and cbuserid = '" + AliApi.appUserId + "'");
            GridView_cata.DataSource = ct.MyTable;
            GridView_cata.DataBind();
        }
        /// <summary>
        /// 绑定正文列表
        /// </summary>
        private void bind2()
        {
            string sql = "";
            sql = " select top 300 * from vcblist where cblisttype = '发帖'  and cbuserid = '" + AliApi.appUserId + "' ";

            if (this.Request.Params["cbusercataid"] != null)
            {
                sql += " and cbusercataid = '" + this.Request.Params["cbusercataid"].ToString() + "' ";
            }
            sql += "  order by jccreatedate desc ,jccreatetime desc ";
            DataTable dt = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];
            DataList_content.DataSource = dt;
            DataList_content.DataBind();
        }
        /// <summary>
        /// 处理request
        /// </summary>
        private void dealrequest()
        {

            if (AliApi.appDomainID != "" && this.Request.Params["userid"] == null)
            {
                this.Response.Redirect(ResolveUrl("~/blog/blogmanage.aspx?userid=")+AliApi.appDomainID);
            }
            if (AliApi.appUserId == "")
            {
                this.Response.Redirect(ResolveUrl("~/blog/default.aspx"));
            }
            if (this.Request.Params["userid"] == null)
            {
                this.Response.Redirect(ResolveUrl("~/blog/default.aspx"));
            }
            if (this.Request.Params["userid"].ToString() != AliApi.appDomainID)
            {
                this.Response.Redirect(ResolveUrl("~/blog/default.aspx"));
            }
            HyperLink_default.NavigateUrl = ResolveUrl("~/blog/blogmanage.aspx?userid=") + this.Request.Params["userid"].ToString();
            cbuser u = cbuser.Find(" recid = " + AliApi.appDomainID);
            if (u.blogtitle.ToString() != "")
            {
                HyperLink_default.Text = u.blogtitle.ToString();
                Literal_desc.Text = u.blogdesc.ToString();
            }
        }

        static public string getstr()
        {
            if (AliApi.appDomainID != "" && HttpContext.Current.Request.Params["userid"] == null)
            {
                System.Web.UI.Control c = new Control();
                HttpContext.Current.Response.Redirect(c.ResolveUrl("~/blog/blogmanage.aspx?userid=") + AliApi.appDomainID);
            }
            if (HttpContext.Current.Request.Params["userid"] == null)
            {
                return "";
            }
            return "&userid=" + HttpContext.Current.Request.Params["userid"].ToString();
        }

        protected void DataList_content_ItemCommand(object source, DataListCommandEventArgs e)
        {
            string id = Convert.ToString(DataList_content.DataKeys[e.Item.ItemIndex]);
            if (e.CommandName == "del")
            {
                cblist l = cblist.Find(" recid = " + id);
                l.Delete();
                bind2();
                // Response.Write("<script>alert('删除成功!')</script>");

            }

        }

        protected void WebPager_content_PageIndexChanged(object sender, RuleCode.Controls.PageChangedEventArgs e)
        {
            bind2();
            this.WebPager_content.CurrentPageIndex = e.NewPageIndex;
        }
    }
}
